<?php

require_once($CONFIG->datadir.'/lib/mainlib.php');

getSessionLang();


function showResults($results, $total, $start) {

    showResultsCount($total);
    showPagination($total, $start);

    showEntries($results);

    showPagination($total, $start);
}


function showResultsCount($results) {

    global $string;

    echo $string['thereare']." ".$results." ".$string['results']."<br /><br />";
}


function showPagination($total, $start) {

    global $CONFIG, $string;

    $searchurl = $CONFIG->webdir.'/search?q='.str_replace(' ', '+', $_SESSION["q"]);
    $thispage = ceil($start/10);
    $npages = ceil($total/10);

    if ($npages != 1 && $npages != 0) {
        for ($i=0 ; $i<$npages ; $i++) {
            $thisstart = $i*10;
            $thisnum = $i + 1;
            if ($i == $thispage) {
                $array[] = $thisnum;
            } else {
                $array[] = '<a href="'.$searchurl.'&start='.$thisstart.'">'.$thisnum.'</a>';
            }
        }
        echo implode(' - ', $array);

    } else {
        return false;
    }
}


function showEntries($entries) {

    if (!empty($entries)) {
        foreach ($entries as $entry) {
            showEntry($entry);
        }
    }
}


function showEntry($entry, $size='40%', $align='center', $border=1) {

    echo '<table align="'.$align.'" width="'.$size.'" border="'.$border.'">';
    echo '<tr><td>';
    echo '<a href="'.$entry->url.'">'.$entry->title.'</a><br>';
    echo $entry->contenttoshow;
    echo '</td></tr>';
    echo '</table>';

}


function showHead() {

    global $CONFIG;

    $stylepath = $CONFIG->webdir.'/styles/'.$CONFIG->style_name.'/style.css';

    $output = '<head>';
    $output .= '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />';
    $output .= '<link rel="stylesheet" type="text/css" href="'.$stylepath.'" />';
    $output .= '</head>';

    echo $output;
}


function showFoot() {

    echo '<br/><br/>';
    echo '<span class="footer">2007 blooglebot && vim</span>';
}


function showTabLinks($destinypath) {

    global $string;

    $output = '<table valign="top" align="left" width="30%"><tr>';

    $tabs = array('t' => $string['titles'], 'l' => $string['labels'], 'v' => $string['videos'], 'i' => $string['images'], 's' => $string['sounds']);


    foreach ($tabs as $id => $text) {

        $output .= '<td align="center">';

        // Check if is the selected tab
        if ($_SESSION['tab'] == $id) {

            $output .= $text;

        } else {

            $tabpath = $destinypath.'?tab='.$id;

            // If q has value we maintain the value
            if ($_SESSION['q']) {
                $tabpath .= '&amp;q='.$_SESSION['q'];
            } 

            $output .= '<a href="'.$tabpath.'">'.$text.'</a>';
        }

        $output .= '</td>';
    }

    $output .= '</tr></table>';

    echo $output;
}


function showLangsLinks($destinypath) {

     global $string;

     $output = '<table align="center" valign="center" width="50%"><tr>';
     $output .= '<td align="center">';

     $output .= '<span class="langslist">';
     $output .= $string['langsinfo'];

     $langsarray = array("en", "es", "ca", "de", "fr", "it", "nl", "jp", "co", "ru", "gr", "pt");

     foreach ($langsarray as $value) {

         if ($_SESSION['lang'] == $value) {
             $langsstring[] = $string[$value];
         } else {
             $langsstring[] = '<a href="'.$destinypath.'?hl='.$value.'">'.$string[$value].'</a>';
         }
     }

     $output .= implode(", ", $langsstring);
     $output .= '</span>';

     $output .= '</td></tr></table>';

     echo $output;
}


?>
